#ifndef __THREADPOOL_H__
#define __THREADPOOL_H__

#include <list>
#include "IMutex.h"
#include "IEvent.h"

class ThreadPool
{
public:
	static ThreadPool *GetThreadPool();
	static void InitThreadPool(int nbThread);
	typedef void (*callback)(void*);
	struct Request
	{	
		bool autoFree;
		callback function;
		void *parameters;
	};
	enum Priority
	{
		HIGH = 2,
		MODERATE = 1,
		LOW = 0
	};

	void AddRequest(Request* request, Priority priority = ThreadPool::MODERATE);

private:
	void   checkThread();
	static ThreadPool* _instance;
	ThreadPool(int nbThread);
	friend void *ThreadProc (void *lpParameter);
	int _maxThread;
	int _curThread;
	int _freeThread;

	std::list<Request*> _highPriorityRequest;
	std::list<Request*> _moderatePriorityRequest;
	std::list<Request*> _lowPriorityRequest;

	IMutex*	_waitingRequest;
	IMutex*	_accessingRequest;
	IMutex*	_infoThread;
	IEvent* _requestAvailable;
};


#endif
